package com.arcvideo.acsconnect.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.arcvideo.acsconnect.ui.MainActivity;
import com.framework.core.utils.AppUtil;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2018/8/21
 * @description
 */

public class BootBroadcastReceiver extends BroadcastReceiver {

    public final static String TAG = BootBroadcastReceiver.class.getSimpleName();
    public final static String ACTION = "android.intent.action.BOOT_COMPLETED";

    private Context mContext;
    private Subscription mSubscription;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Log.d(TAG, "onReceive() action : " + intent.getAction());
        if (intent.getAction().equals(ACTION)) {
            mSubscription = Observable.interval(1, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Long>() {
                        @Override
                        public void call(Long aLong) {
                            if (AppUtil.isNetworkConnected(mContext)) {
                                MainActivity.launchFromBoot(mContext);
//                                AcsConnectApplication.sIsLoginFromBoot = true;
//                                Intent loginIntent = new Intent(mContext, MainActivity.class);
//                                loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                mContext.startActivity(loginIntent);
                                mSubscription.unsubscribe();
                            }
                        }
                    });
        }
    }

}
